package com.jingdianjichi.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jingdianjichi.oss.entity.Result;
import com.jingdianjichi.oss.service.FileService;
import com.jingdianjichi.oss.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class FileController {

    @NacosValue(value = "${storage.service.type}",autoRefreshed = true)
    private String storageType;
    @Resource
    private FileService fileService;

    @RequestMapping("/testGetAllBuckets")
    public String testGetAllBuckets() throws Exception {
        List<String> allBucket = fileService.getAllBucket();
        return allBucket.get(0);
    }

    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }

    /**
     * 上传文件
     */
//    @RequestMapping("/upload")
//    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
////        String url = fileService.uploadFile(uploadFile, bucket, objectName);
////        return Result.ok(url);
//        log.info("文件上传，{}",uploadFile);
//        String url = fileService.upload(uploadFile);
//           return   Result.ok(url);
//    }
    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile uploadFile) throws Exception {
//        String url = fileService.uploadFile(uploadFile, bucket, objectName);
//        return Result.ok(url);
        log.info("文件上传aliyun，{}",uploadFile);
        String url = fileService.upload(uploadFile);
        return   Result.ok(url);
    }

}
